const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const env = process.env.NODE_ENV === 'production' ? require('./env/production') : require('./env/development');
const config = {
	entry: path.resolve(__dirname, './src/index.js'),
	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: '[name].bundle.js'
	},
	resolve: {
		extensions: ['.js', '.jsx', '.json']
	},
	module: {
		rules: [
			{
				test: /\.(js|jsx)$/,
				exclude: /node_modules/,
				use: [{
					loader: 'babel-loader',
					options: {
						presets: ['react', 'es2015'],
						plugins: ['react-hot-loader/babel', ['import', { libraryName: 'antd', style: 'css' }]]
					}
				}]
			}, {
				test: /\.css$/,
				use: ['css-hot-loader', MiniCssExtractPlugin.loader, 'css-loader']
			}, {
				test: /\.less$/,
				use: ['css-hot-loader', MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
			},
			{
				test: /\.(jpg|png|gif|svg)$/,
				use: [
					{
						loader: 'file-loader',
						options: { name: 'images/[name].[ext]' }
					}
				]
			},
			{
				test: /\.(woff|woff2|svg|ttf|eot)$/,
				use: [
					{
						loader: 'file-loader',
						options: { name: 'fonts/[name].[hash:8].[ext]' }
					}
				]
			}
		]
	},
	devtool: 'inline-source-map',
	devServer: {
		port: 9000,
		hot: true,
		open: true,
		compress: true,
		inline: true,
		contentBase: './dist',
	},
	plugins: [
		new CleanWebpackPlugin(['dist']),
		new HtmlWebpackPlugin({
			title: 'react',
			template: path.resolve(__dirname, 'index.html')
		}),
		new MiniCssExtractPlugin('styles.css'),
		new webpack.HotModuleReplacementPlugin()
	]
};

module.exports = merge(config, env);